package servlet.dowmload;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

@WebServlet("/d01.do")
public class d01 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)   {
        try {

            ServletOutputStream outputStream = resp.getOutputStream();
            FileInputStream fis = new FileInputStream(new File("D:\\idea_java\\m03\\m03_1\\src\\main\\resources\\a0.jpg"));
            resp.setHeader("content-disposition", "attachment;fileName=" + "a0.jpg");
            byte[] bytes = new byte[1024];
            int length;
            while((length = fis.read(bytes))!=-1){
                outputStream.write(bytes,0,length);
                outputStream.flush();
            }
            outputStream.close();
            fis.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
